package zheezes.protocol.impl.wt.du;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import zheezes.protocol.impl.wt.DataUnit;
import zheezes.protocol.impl.wt.meta.FrzDate;
import zheezes.protocol.impl.wt.meta.WtMtAddr;


public class Ctrl1EDi0243DataUnit extends DataUnit {
	public static final int MAX_MT_NUM = 8;

	private FrzDate date = new FrzDate();
	private List<WtMtAddr> list = new ArrayList<WtMtAddr>();
	
	public void setFrzDate(Date d) {
		date.setDate(d);
	}

	public int addMt(WtMtAddr addr) {
		if (list.size() < 8) {
			list.add(addr);
			return 0;
		}
		return -1;
	}

	@Override
	public int getData(byte[] buf, int offset) {
		int idx = offset;
		idx += date.getBytes(buf, idx);
		for (WtMtAddr a : list) {
			idx += a.getBytes(buf, idx);
		}
		return idx - offset;
	}

	@Override
	public int setData(byte[] buf, int offset, int max) {
		int idx = offset;
		idx += date.setBytes(buf, idx, max);
		list.clear();
		while (idx < max) {
			WtMtAddr a = new WtMtAddr();
			idx += a.setBytes(buf, idx, max);
			list.add(a);
		}
		return idx - offset;
	}

	@Override
	public int sizeOfData() {
		return date.size() + list.size() * 5;
	}
}
